package net.mymilkedeek.ludum.tinythumb;

import net.mymilkedeek.ludum.tinythumb.text.SpeakerText;
import net.mymilkedeek.ludum.tinythumb.util.Constants;
import net.mymilkedeek.ludum.tinythumb.util.URL;
import org.newdawn.slick.*;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.util.Log;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO: Write Documentation
 *
 * @author Michael Demey
 */
public class ActionSpace {

    private Image textBox;
    private Map<String, Image> speakerMap;
    private SpeakerText[] speakers;
    private Animation arrow;
    private TextFactory textFactory;
    private int current;
    private Sound textBlip;
    public String currentString;

    private boolean drawn = false;
    
    private int textLocX = 120;
    private int textLocY = 80;

    public ActionSpace() throws SlickException {
        textBox = new Image("/resources/text/text.png");
        arrow = new Animation(new SpriteSheet("/resources/text/arrow.png", 50, 50), 500);
        textFactory = new TextFactory();
        speakerMap = new HashMap<String, Image>();
        textBlip = new Sound(URL.sfx_text_blip);
    }
    
    public void init(String textId) throws SlickException {
        currentString = textId;
        Log.debug("initActionSpace " + textId);
        speakers = textFactory.getText(textId);
        String currentSpeaker;

        for ( SpeakerText speakerText : speakers ) {
            currentSpeaker = speakerText.getSpeaker();
            
            if ( ! speakerMap.containsKey(currentSpeaker)) {
                speakerMap.put(currentSpeaker, new Image(URL.text_resources + currentSpeaker + ".png"));
            }
        }

        current = 0;
    }

    public void render(GameContainer gameContainer, StateBasedGame stateBasedGame, Graphics graphics) throws SlickException {
        graphics.setColor(Color.black);
        textBox.draw(0, Constants.screen_height - 100);
        speakerMap.get(speakers[current].getSpeaker()).draw(1, Constants.screen_height - 101);
        arrow.draw(Constants.screen_width - 51, Constants.screen_height - 51);

        String[] text = speakers[current].getText();
        
        graphics.drawString(text[0], textLocX, Constants.screen_height - textLocY);
        graphics.drawString(text[1], textLocX, Constants.screen_height - ( textLocY / 2 ));

        drawn = true;
    }

    public boolean update(GameContainer gameContainer, StateBasedGame stateBasedGame, int delta) throws SlickException {
        if ( drawn )
            current++;

        textBlip.play();

        if ( current >= speakers.length) {
            current = 0;
            drawn = false;
            return false;
        }
        return true;
    }
}